#include "Database.h"
#include <QVariant>
Database::Database()
{
#ifdef MAIL_TEST
     dataLocationPath = QDir::currentPath();
#else
     dataLocationPath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
#endif
     dataLocationDir.setPath(dataLocationPath);

}
QStringList Database::GetDatabaseList()
{
    if(dataLocationDir.exists())
    {
        dataLocationDir.setNameFilters(QStringList("*.db"));
        QStringList databaseList = dataLocationDir.entryList();
        return databaseList;
    }
    else
    {
        qDebug("Directory does not exist.");
        return QStringList();
    }
}

void Database::CreateDatabase(QString account)
{
    QString connectName;
    QString dbFileName = dataLocationDir.path() + "/" + account + ".db";
    if(dataLocationDir.exists())
    {
        if(!QFile::exists(dbFileName))
        {
            QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
            db.setDatabaseName(dbFileName);
            if(db.open())
            {
                connectName = db.connectionName();
                QSqlQuery query;
                if(query.exec("CREATE TABLE IF NOT EXISTS mail (id INTEGER UNIQUE PRIMARY KEY, email_contact VARCHAR(30), title TEXT, attachment TEXT, date VARCHAR(30))")
                            && query.exec("CREATE TABLE IF NOT EXISTS contact (id INTEGER UNIQUE PRIMARY KEY, name VARCHAR(30), email VARCHAR(30))"))
                    qDebug("Create database");
                else
                    qDebug("Fail to create table");


            }
            else
                qDebug("Could not open database");
            db.close();
        }
        else
            qDebug("Database exists");

    }
    else
    {
        if(dataLocationDir.mkpath(dataLocationDir.path()))
        {
            QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
            db.setDatabaseName(dbFileName);
            if(db.open())
            {
                connectName = db.connectionName();
                QSqlQuery query;
                if(query.exec("CREATE TABLE IF NOT EXISTS mail (id INTEGER UNIQUE PRIMARY KEY, email_contact VARCHAR(30), title TEXT, attachment TEXT, date VARCHAR(30))")
                            && query.exec("CREATE TABLE IF NOT EXISTS contact (id INTEGER UNIQUE PRIMARY KEY, name VARCHAR(30), email VARCHAR(30))"))
                    qDebug("Create database");
                else
                    qDebug("Fail to create table");


            }
            else
                qDebug("Could not open database");
            db.close();
        }
        else
            qDebug("Fail to create directory.");

    }
    QSqlDatabase::removeDatabase(connectName);
}

bool Database::DeleteDatabase(QString account)
{
    QString dbName = dataLocationDir.path() + "/" + account + ".db";
    return QFile::remove(dbName);

}


bool Database::InsertRecordToTable(QString record)
{
    return true;
}

QStringList Database::EmailContact(QString nameDatabase)
{

    QString dbFileName = dataLocationDir.path() + "/" + nameDatabase + ".db";
    QString connectName;
    QStringList i;
    if(QFile::exists(dbFileName))
    {
        QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );
        db.setDatabaseName(dbFileName);
        connectName = db.connectionName();
        if(db.open())
        {
            QSqlQuery sqlQuery;
            QString query = "SELECT email FROM contact";
            if(sqlQuery.exec(query))
            {
                while(sqlQuery.next())
                {
                    i.append(sqlQuery.value(0).toString());
                }

            }
            else
                qDebug("Wrong query");
        }
        else
            qDebug("Could not open database");
        db.close();
    }
    else
        qDebug("Database not exists");

    QSqlDatabase::removeDatabase(connectName);

    return i;
}

bool Database::DeleteRecordOfTable(QString record)
{
    return true;
}

